package com.ctshk.rpc.order.scenic.service.impl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.DateUtil;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.order.scenic.entity.ScenicOrder;
import com.ctshk.rpc.order.scenic.entity.ScenicOrderReceipt;
import com.ctshk.rpc.order.scenic.mapper.ScenicOrderMapper;
import com.ctshk.rpc.order.scenic.mapper.ScenicOrderReceiptMapper;
import com.ctshk.rpc.order.scenic.req.ScenicOrderReceiptAddReq;
import com.ctshk.rpc.order.scenic.req.ScenicOrderReceiptModifyReq;
import com.ctshk.rpc.order.scenic.service.IScenicOrderReceiptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.rpc.system.dto.UserDTO;
import com.ctshk.rpc.system.service.ISysUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Calendar;

/**
 * <p>
 * 景点门票订单-收据表 服务实现类
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-03-25
 */
@DubboService
public class ScenicOrderReceiptServiceImpl extends ServiceImpl<ScenicOrderReceiptMapper, ScenicOrderReceipt> implements IScenicOrderReceiptService {

    @DubboReference
    private ISysUserService iSysUserService;

    @Autowired
    ScenicOrderReceiptMapper scenicOrderReceiptMapper;

    @Autowired
    ScenicOrderMapper scenicOrderMapper;

    @Override
    public Result<Long> modify(ScenicOrderReceiptModifyReq scenicOrderReceiptModifyReq, TokenUser tokenUser) {
        ScenicOrderReceipt scenicOrderReceipt = EntityUtil.copy(scenicOrderReceiptModifyReq, ScenicOrderReceipt.class);
        scenicOrderReceipt.setModifiedId(tokenUser.getId());
        scenicOrderReceipt.setGmtModified(LocalDateTime.now());
        return Result.success(scenicOrderReceiptModifyReq.getOrderId());
    }

    @Override
    public Result<Long> add(ScenicOrderReceiptAddReq scenicOrderReceiptAddReq, TokenUser tokenUser) {
        //生成收据
        ScenicOrderReceipt scenicOrderReceipt = new ScenicOrderReceipt();
        scenicOrderReceipt.setOrderId(scenicOrderReceiptAddReq.getOrderId());
        //門市編號+員工編號+年後2位+月日4位+A+時分秒6位+（S）
        Calendar cal = Calendar.getInstance();
        int hour=cal.get(Calendar.HOUR);//小时
        int minute=cal.get(Calendar.MINUTE);//分
        int second=cal.get(Calendar.SECOND);//秒
        String hourNew = String.valueOf(hour);
        String minuteNew = String.valueOf(minute);
        String secondNew = String.valueOf(second);
        if(hourNew.length() <2 ){
            hourNew = "0" + hourNew;
        }
        if(minuteNew.length() <2 ){
            minuteNew = "0" + minuteNew;
        }
        if(secondNew.length() <2 ){
            secondNew = "0" + secondNew;
        }
        String time =  hourNew + minuteNew + secondNew;
        UserDTO userDTO = iSysUserService.loadUserByParam(tokenUser.getId(), 1);
        String receiptNo = userDTO.getDept() + userDTO.getEmployeeNumber() + DateUtil.fmtNum(LocalDate.now()) + "A" + time + "(s)";
        scenicOrderReceipt.setReceiptNo(receiptNo);
        Long id = SnowflakeIdWorker.nextId();
        scenicOrderReceipt.setId(id);
        scenicOrderReceipt.setCreateId(tokenUser.getId());
        scenicOrderReceipt.setGmtCreate(LocalDateTime.now());
        scenicOrderReceipt.setBranch(userDTO.getEmployeeNumber() + "-" + userDTO.getDept());
        ScenicOrder scenicOrder = scenicOrderMapper.selectById(scenicOrderReceiptAddReq.getOrderId());
        scenicOrderReceipt.setGuest(scenicOrder.getContactName());
        scenicOrderReceipt.setPhone(scenicOrder.getContactMobile());
        scenicOrderReceiptMapper.insert(scenicOrderReceipt);
        return Result.success(id);
    }

}
